﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using DFGK_WMS.WebAPI.Tools;
using Dm.Config;
using Microsoft.AspNetCore.Mvc;
using System.Net.Mime;

namespace DFGK_WMS.WebAPI.Controllers.库存管理
{
    /// <summary>
    /// 库存流水
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TInventoryLogsController : ControllerBase
    {
        private readonly ITInventoryLogsRepository InventoryLogs;

         public TInventoryLogsController(ITInventoryLogsRepository InventoryLogs)
        {
            this.InventoryLogs = InventoryLogs;
        }


        /// <summary>
        /// 查询库存流水
        /// </summary>
        /// <param name="WarehouseId"></param>
        /// <param name="AreaId"></param>
        /// <param name="SourceType"></param>
        /// <param name="MaterialId"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryflow(int? WarehouseId, int? AreaId, int? SourceType, string? MaterialName,
            string? StartTime, string? EndTime, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            var list = InventoryLogs.GetInventoryflow(out var totalCount , WarehouseId, AreaId, SourceType, MaterialName, StartTime, EndTime, pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));

            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 枚举-库存流水单据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEmInBillType()
        {
            var list = BaseEnum.GetCodeRulesList<EmSourceType>();

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "获取成功！", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }


        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public FileContentResult ExportToExcel(string? name)
        {
            var list = InventoryLogs.GetAllInventoryLog();
            var keyValuePairs = new Dictionary<string, string>()
            {
                { "SourceType","来源类型" },
                { "SourceSn","来源单号"},
                { "MaterialName","物料名称"},
                { "MaterialCode","物料编码"},
                { "StartQty","原始数量"},
                {"ChangeQty","变更数量" },
                {"EndQty","最终数量" },
                {"CreateDate","生成日期" },
                { "PalletBarcode","托盘号" },
                {"LocationCode","仓位" }
            };
            var exList = list.Select(s => new
            {
                s.SourceType,
                s.SourceSn,
                s.MaterialName,
                s.MaterialCode,
                s.StartQty,
                s.ChangeQty,
                s.EndQty,
                s.CreateDate,
                s.PalletBarcode,
                s.LocationCode
            }).ToList();
            var fileBytes = Utils.ExportToExcel(exList, keyValuePairs);
            return File(fileBytes, MediaTypeNames.Application.Octet, "客户.xlsx");
        }
    }
}
